define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, undefined, Echarts, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格參數配置
            Table.api.init({
                extend: {
                    index_url: 'aps/mainprocess/index',
                    add_url: 'aps/mainprocess/add',
                    edit_url: '',
                    del_url: 'aps/mainprocess/del',
                    multi_url: 'aps/mainprocess/multi',
                    table: 'aps_mainprocess',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                showToggle: false,
                sortName: 'id',
                searchFormVisible: true,
                showToggle: false,
                showColumns: false,
                showExport: false,
                search: false,
                dblClickToEdit: false, //是否啟用雙擊編輯
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('Id'),  sortable: true},
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        //{field: 'number', title: __('Number')},
                        //{field: 'mtype', title: __('Mtype')},
                        {field: 'manhours', title: __('Manhours'), operate:false, visible:Config.fyw},
                        {field: 'lydays', title: __('來料採購最長天數'), operate:false},
                        {field: 'sczq', title: __('生產周期'), operate:false},
                        //{field: 'state', title: __('State'), searchList: {"0":__('State 0'),"1":__('State 1')}, formatter: Table.api.formatter.normal},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            visible: Config.fyw,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    title: __('管理'),
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa-wrench',
                                    url: 'aps/procedure/index'
                                },
                                {
                                    name: 'addtabs',
                                    title: __('BOM'),
                                    classname: 'btn btn-xs btn-info btn-addtabs',
                                    icon: 'fa fa-database',
                                    url: 'aps/mainprocessbom/index'
                                },
                                //{
                                //    name: 'addtabs',
                                //    title: __('產品交期預估'),
                                //    classname: 'btn btn-xs btn-warning btn-addtabs',
                                //    icon: 'fa fa-yelp',
                                //    url: 'aps/mainprocess/cpjqyg'
                                //}
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 為表格綁定事件
            Table.api.bindevent(table);
        },
        gstj: function () {

            var myChart = Echarts.init(document.getElementById('echart'), 'walden');

            var posList = [
                'left', 'right', 'top', 'bottom',
                'inside',
                'insideTop', 'insideLeft', 'insideRight', 'insideBottom',
                'insideTopLeft', 'insideTopRight', 'insideBottomLeft', 'insideBottomRight'
            ];

            var app = [];
            app.configParameters = {
                rotate: {
                    min: -90,
                    max: 90
                },
                align: {
                    options: {
                        left: 'left',
                        center: 'center',
                        right: 'right'
                    }
                },
                verticalAlign: {
                    options: {
                        top: 'top',
                        middle: 'middle',
                        bottom: 'bottom'
                    }
                },
                position: {
                    options: Echarts.util.reduce(posList, function (map, pos) {
                        map[pos] = pos;
                        return map;
                    }, {})
                },
                distance: {
                    min: 0,
                    max: 100
                }
            };

            app.config = {
                rotate: 90,
                align: 'left',
                verticalAlign: 'middle',
                position: 'insideBottom',
                distance: 10,
                onChange: function () {
                    var labelOption = {
                        normal: {
                            rotate: app.config.rotate,
                            align: app.config.align,
                            verticalAlign: app.config.verticalAlign,
                            position: app.config.position,
                            distance: app.config.distance
                        }
                    };
                    myChart.setOption({
                        series: [{
                            label: labelOption
                        }, {
                            label: labelOption
                        }, {
                            label: labelOption
                        }, {
                            label: labelOption
                        }]
                    });
                }
            };

            var labelOption = {
                normal: {
                    show: true,
                    position: app.config.position,
                    distance: app.config.distance,
                    align: app.config.align,
                    verticalAlign: app.config.verticalAlign,
                    rotate: app.config.rotate,
                    formatter: '{c}  {name|{a}}',
                    fontSize: 12,
                    rich: {
                        name: {
                            textBorderColor: '#fff'
                        }
                    }
                }
            };

            var xlist = [];
            var y1list = [];
            var y2list = [];
            var y3list = [];
            var mainprocessname = "";
            for (var i = 0; i < Statsdata.list.length; i++) {
                //xlist.push(Statsdata.list[i].mainprocessname + '\n' + Statsdata.list[i].procedurename);
                if (i == 0) {
                    mainprocessname = Statsdata.list[i].mainprocessname;
                }
                xlist.push(Statsdata.list[i].procedurename);
                y1list.push(Math.ceil(Statsdata.list[i].MaxManhours * 60));
                y2list.push(Math.ceil(Statsdata.list[i].AVGManhours * 60));
                y3list.push(Math.ceil(Statsdata.list[i].MinManhours * 60));
            }

            option = {
                title: {
                    text: "產品名：" + mainprocessname,
                    subtext: ' 單位:分鐘',
                    left: 'left'
                },
                //color: ['#003366', '#006699', '#4cabce', '#e5323e'],
                color: ['#003366', '#006699', '#4cabce'],
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                legend: {
                    //data: ['Forest', 'Steppe', 'Desert', 'Wetland']
                    data: ['最大', '平均', '最小']
                },
                toolbox: {
                    show: true,
                    orient: 'vertical',
                    left: 'right',
                    top: 'center',
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        axisTick: {show: false},
                        data: xlist,
                        axisLabel: {
                            interval: 0,//橫軸信息全部顯示
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '最大',
                        type: 'bar',
                        barGap: 0,
                        label: labelOption,
                        //data: [320, 332, 301, 334, 390]
                        data: y1list
                    },
                    {
                        name: '平均',
                        type: 'bar',
                        label: labelOption,
                        //data: [220, 182, 191, 234, 290]
                        data: y2list
                    },
                    {
                        name: '最小',
                        type: 'bar',
                        label: labelOption,
                        //data: [150, 232, 201, 154, 190]
                        data: y3list
                    }
                ]
            };

            myChart.setOption(option);

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        cpjqyg: function () {

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});